package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationState;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.ui.BadgeView;
import com.cn.xact.ty_treasured_book.ui.ItemView;
import com.cn.xact.ty_treasured_book.util.UIHelper;
import com.cn.xact.ty_treasured_book.TYApplicationModule;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button bt;
    BadgeView bgView;

    ImageView searchView;
    ImageView msgView;
    ImageView homeView;
    ImageView setView;
    ImageView addView;

    Button test;

    private final static int TY_MAX_ITEMS_ON_MAIN_PAGE = 6;
    private ViewPager viewPager;
    private ArrayList<ViewGroup> pageViews = new ArrayList<ViewGroup>();
    private ViewGroup main;
    private TextView pageNum;
    private NetworkImpl mNewMsgNetwork;

    public static TYApplicationState app;

    private ArrayList<ItemView> mItemViews = new ArrayList<ItemView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        LayoutInflater inflater = getLayoutInflater();

        app = new TYApplicationState(this);

        initModuels(inflater);

        main = (ViewGroup)inflater.inflate(R.layout.activity_main, null);
        viewPager = (ViewPager)main.findViewById(R.id.mainPager);

        setContentView(main);

        pageNum = (TextView)main.findViewById(R.id.page_number);

        pageNum.setText("1");

        searchView = (ImageView)main.findViewById(R.id.search);
        msgView    = (ImageView)main.findViewById(R.id.msg);
        homeView   = (ImageView)main.findViewById(R.id.home);
        setView    = (ImageView)main.findViewById(R.id.set);
        addView    = (ImageView)main.findViewById(R.id.add);

        viewPager.setAdapter(new MainPageAdapter());
        viewPager.setOnPageChangeListener(new MainPageChangeListener());

        initListenerForOPModules();
    }

    @Override
    public void onResume() {
        super.onResume();
        mNewMsgNetwork = new NetworkImpl(this, new MyNetworkCallBack());
        mNewMsgNetwork.getNewMsgAsync(123);
    }

    private void initModuels(LayoutInflater inflater) {
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        int width = (wm.getDefaultDisplay().getWidth() * 7 / 20);
        int moduleNum;

        for (final TYApplicationModule tm : app.getModuleList()) {
            ItemView iv = null;
            if ((tm.isEnable()) && (!tm.hasChild())) {
                iv = new ItemView(this, tm.getDisplayName(), width, width, tm.getIconId());
                iv.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        if ((tm.getAction() != null) && (!tm.getAction().equals(""))) {
                            Intent tmp = new Intent();
                            tmp.setAction(tm.getAction());
                            startActivity(tmp);
                            //MainActivity.this.finish();
                        }
                    }
                });

                mItemViews.add(iv);
            }
        }

        moduleNum = mItemViews.size();
        int pageCounter = (int)Math.ceil(((float)moduleNum / (float)TY_MAX_ITEMS_ON_MAIN_PAGE));

        for (int i = 0; i < pageCounter; i++) {
            ViewGroup main = (ViewGroup)inflater.inflate(R.layout.main_modules, null);
            for (int j = 0; j < TY_MAX_ITEMS_ON_MAIN_PAGE; j++) {
                int position = i * TY_MAX_ITEMS_ON_MAIN_PAGE + j;
                if (position < moduleNum) {
                    ItemView myIV = mItemViews.get(position);
                    main.addView(mItemViews.get(position));
                    myIV.setMargin();

                    if (j < 2)
                        myIV.setImportant();
                }
            }
            pageViews.add(main);
        }
    }

    private void initListenerForOPModules() {
        searchView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent tmp = new Intent(MainActivity.this, SearchPage.class);
                startActivity(tmp);
            }
        });

        msgView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent tmp = new Intent(MainActivity.this, MsgListActivity.class);
                startActivity(tmp);
            }

        });

        homeView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent tmp = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(tmp);
            }

        });

        setView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent tmp = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(tmp);
            }

        });

        addView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent tmp = new Intent(MainActivity.this, AddEntryActivity.class);
                startActivity(tmp);
                MainActivity.this.finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class MainPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
    }

    class MainPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            pageNum.setText(String.valueOf(arg0 + 1));
        }

    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(MainActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_NEW_MSG:
                BadgeView badgeAgent = new BadgeView(msgView.getContext(), msgView);
                badgeAgent.setText(String.valueOf((Integer)attach1));
                badgeAgent.show();
                break;
            }
        }
    }

}
